package com.hyg.module_health.view.activity.HealthAdviseActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.DataUtil.UserSPUtils;
import com.hyg.lib_common.constant.FileConstants;
import com.hyg.module_health.R;
import com.hyg.module_health.databinding.ActivityHealthAdviceListBinding;
import com.hyg.module_report.adapter.HealthCareAdapter;
import com.hyg.module_report.model.AdviceShowItemData;
import com.hyg.module_report.model.V2AdviceDetailData;
import com.hyg.module_report.ui.activity.HealthAdviceDetailActivity;

import java.util.ArrayList;
import java.util.List;

public class HealthAdviceListActivity extends BaseActivity {
    ActivityHealthAdviceListBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHealthAdviceListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initview();
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshRV_YSJY();
    }

    public void initview() {
        StatusBarUtil.setStatusBar(1, this, getResources().getColor(R.color.white), 00, null, null, true);
        binding.tvTitle.setText("养生建议");
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * description:  显示养生建议信息列表
     *
     * @author visitant
     * @time 2020/12/14 15:39
     */
    public void refreshRV_YSJY() {
        List<V2AdviceDetailData> shanShiList = JSONArray.parseArray(UserSPUtils.getUserString(this, FileConstants.Yinshi_data), V2AdviceDetailData.class);
        List<V2AdviceDetailData> chayinList = JSONArray.parseArray(UserSPUtils.getUserString(this, FileConstants.ChaYin), V2AdviceDetailData.class);
        if (chayinList != null && chayinList.size() > 0) {
            for (V2AdviceDetailData data : chayinList) {
                shanShiList.add(data);
            }
        }
        if (shanShiList != null && shanShiList.size() > 0) {
            initRecyclerView(this, binding.SSHealthCare, (ArrayList<V2AdviceDetailData>) shanShiList);
            binding.tvSsys.setVisibility(View.VISIBLE);
            binding.SSHealthCare.setVisibility(View.VISIBLE);
        } else {
            binding.tvSsys.setVisibility(View.GONE);
            binding.SSHealthCare.setVisibility(View.GONE);
        }

        List<V2AdviceDetailData> yaoYuList = JSONArray.parseArray(UserSPUtils.getUserString(this, FileConstants.Zuyu_data), V2AdviceDetailData.class);
        if (yaoYuList != null && yaoYuList.size() > 0) {
            initRecyclerView(this, binding.YYHealthCare, (ArrayList<V2AdviceDetailData>) yaoYuList);
            binding.tvYyys.setVisibility(View.VISIBLE);
            binding.YYHealthCare.setVisibility(View.VISIBLE);
        } else {
            binding.tvYyys.setVisibility(View.GONE);
            binding.YYHealthCare.setVisibility(View.GONE);
        }

        List<V2AdviceDetailData> yongDongList = JSONArray.parseArray(UserSPUtils.getUserString(this, FileConstants.YunDong_data), V2AdviceDetailData.class);
        if (yongDongList != null && yongDongList.size() > 0) {
            initRecyclerView(this, binding.CYHealthCare, (ArrayList<V2AdviceDetailData>) yongDongList);
            binding.tvCyys.setVisibility(View.VISIBLE);
            binding.CYHealthCare.setVisibility(View.VISIBLE);
        } else {
            binding.tvCyys.setVisibility(View.GONE);
            binding.CYHealthCare.setVisibility(View.GONE);
        }

        List<V2AdviceDetailData> aiJiuList = JSONArray.parseArray(UserSPUtils.getUserString(this, FileConstants.Liliao_data), V2AdviceDetailData.class);
        if (aiJiuList != null && aiJiuList.size() > 0) {
            binding.tvAjys.setVisibility(View.VISIBLE);
            binding.AJHealthCare.setVisibility(View.VISIBLE);
            initRecyclerView(this, binding.AJHealthCare, (ArrayList<V2AdviceDetailData>) aiJiuList);
        } else {
            binding.tvAjys.setVisibility(View.GONE);
            binding.AJHealthCare.setVisibility(View.GONE);
        }
    }


    /**
     * description:  初始化单项RecyclerView显示
     *
     * @author visitant
     * @time 2020/12/14 15:40
     */
    @SuppressLint("WrongConstant")
    public void initRecyclerView(Activity activity, RecyclerView view, ArrayList<V2AdviceDetailData> list) {
        ArrayList<AdviceShowItemData> itemlist = new ArrayList<>();
        for (V2AdviceDetailData data : list) {
            itemlist.add(new AdviceShowItemData(data.getYsName(), data.getImageUrl()));
        }
        HealthCareAdapter adapter = new HealthCareAdapter(activity, itemlist);
        view.setNestedScrollingEnabled(false);
        LinearLayoutManager mLayoutManage = new LinearLayoutManager(activity);
        mLayoutManage.setOrientation(OrientationHelper.HORIZONTAL);//设置滚动方向，横向滚动
        view.setLayoutManager(mLayoutManage);
        view.setAdapter(adapter);
        view.setItemAnimator(new DefaultItemAnimator());
        adapter.setOnItemClickLitener(new HealthCareAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent it = new Intent(activity, HealthAdviceDetailActivity.class);
                it.putExtra("AdviceData", list.get(position));
                startActivity(it);
                activity.overridePendingTransition(R.anim.fade, R.anim.hold);
            }

            public void onItemLongClick(View view, int position) {
            }
        });

    }


}